using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Domain.Entities;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using JGSY.CMS.LowCode.Platform.Infrastructure.CmsDbContext;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Repositories
{
    /// <summary>
    /// 安全事件仓储实现
    /// 提供系统安全事件记录和查询的数据访问功能实现，使用内存存储
    /// </summary>
    public class SecurityEventRepository : ISecurityEventRepository
    {
        private readonly LowCodeDbContext _context;
        /// <summary>
        /// 安全事件记录集合
        /// </summary>
        private readonly List<SecurityEvent> _list = new();

        public SecurityEventRepository(LowCodeDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取所有安全事件记录
        /// </summary>
        /// <returns>安全事件记录集合</returns>
        public async Task<IEnumerable<SecurityEvent>> GetAllAsync()
        {
            await Task.CompletedTask;
            return _list;
        }

        /// <summary>
        /// 根据标识符获取安全事件记录
        /// </summary>
        /// <param name="id">安全事件标识符</param>
        /// <returns>安全事件记录，如果不存在则返回null</returns>
        public async Task<SecurityEvent?> GetByIdAsync(long id)
        {
            await Task.CompletedTask;
            return _list.FirstOrDefault(e => e.Id == id);
        }

        /// <summary>
        /// 添加新的安全事件记录
        /// </summary>
        /// <param name="entity">要添加的安全事件实体</param>
        /// <returns>添加操作的异步任务</returns>
        public async Task AddAsync(SecurityEvent entity)
        {
            await Task.CompletedTask;
            _list.Add(entity);
        }

        /// <summary>
        /// 更新安全事件记录
        /// </summary>
        /// <param name="id">安全事件标识符</param>
        /// <param name="entity">更新后的安全事件实体</param>
        /// <returns>更新操作的异步任务</returns>
        public async Task UpdateAsync(long id, SecurityEvent entity)
        {
            await Task.CompletedTask;
            var idx = _list.FindIndex(e => e.Id == id);
            if (idx >= 0) _list[idx] = entity;
        }

        /// <summary>
        /// 删除安全事件记录
        /// </summary>
        /// <param name="id">安全事件标识符</param>
        /// <returns>删除操作的异步任务</returns>
        public async Task DeleteAsync(long id)
        {
            await Task.CompletedTask;
            _list.RemoveAll(e => e.Id == id);
        }
    }
}
